﻿using Api.BLL;
using Api.Model;
using System;
using System.Linq;
using System.Web.Mvc;
using Api.Common;
using Api.Web.helper;

namespace Api.Web.Areas.Admin.Controllers
{
    public class MethodParameterController : AdminController
    {
        public ActionResult Create(int? methodId)
        {
            if (methodId == null)
            {
                ViewBag.Error = "參數錯誤";
            }
            return View();
        }

        public ActionResult Edit(int? methodId, int? methodParameterId)
        {
            if (methodId == null || methodParameterId == null)
            {
                ViewBag.Error = "參數錯誤";
            }
            var parameter = MethodParameterBLL.Current.GetModel(s => s.MethodId == methodId && s.Id == methodParameterId && s.UserId == userId && s.ProjectId == projectId);
            if (parameter == null) return View("_404");
            return View("Create", parameter);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Edit(MethodParameter methodParameter)
        {
            if (methodParameter.MethodId == 0)
            {
                AjaxMsgHelper.ResponseFail("缺少methodId");
                return;
            }
            bool flag = true;
            if (!string.IsNullOrEmpty(methodParameter.Name))
            {
                if (methodParameter.Id > 0)
                {
                    try
                    {
                        MethodParameterBLL.Current.UpdateIgnore(s => new { s.CreateDate, s.MethodId, s.UserId }, methodParameter);
                    }
                    catch
                    {
                        flag = false;
                    }
                }
                else
                {
                    try
                    {
                        methodParameter.ProjectId = projectId;
                        methodParameter.UserId = userId;
                        methodParameter.CreateDate = DateTime.Now;
                        MethodParameterBLL.Current.Add(methodParameter);
                    }
                    catch
                    {
                        flag = false;
                    }
                }

                if (flag)
                {
                    AjaxMsgHelper.ResponseSuccess("操作成功");
                }
                else
                {
                    AjaxMsgHelper.ResponseFail("操作失敗，請重試");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("請填寫名稱");
            }
        }

        public ActionResult List(int? methodId)
        {
            if (methodId == null)
            {
                ViewBag.Error = "參數錯誤";
                return View();
            }
            var list = MethodParameterBLL.Current.GetList(s => s.MethodId == methodId.Value && s.UserId == userId);
            if (list == null)
            {
                ViewBag.Error = "數據庫查詢錯誤";
                return View();

            }
            return PartialView("list", list);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public void Delete(params int[] ids)
        {
            if (ids.Length > 0)
            {
                var list = MethodParameterBLL.Current.GetList(s => ids.Contains(s.Id) && s.UserId == userId);
                if (list == null)
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                    return;
                }
                bool result = true;
                try
                {
                    MethodParameterBLL.Current.DeleteList(list);
                }
                catch
                {
                    result = false;
                }
                if (result)
                {
                    AjaxMsgHelper.ResponseSuccess("刪除成功");
                }
                else
                {
                    AjaxMsgHelper.ResponseFail("操作失敗");
                }
            }
            else
            {
                AjaxMsgHelper.ResponseFail("請選擇一項");
            }
        }
    }
}